<?php

//字符串处理
function daddslashes($string, $force = 0, $strip = FALSE) {
	!defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
	if(!MAGIC_QUOTES_GPC || $force) {
		if(is_array($string)) {
			foreach($string as $key => $val) {
				$string[$key] = daddslashes($val, $force, $strip);
			}
		} else {
			$string = addslashes($strip ? stripslashes($string) : $string);
		}
	}
	return $string;
}
//检测网站是否能访问
function httpcodeCheck($url) {
	$ch = curl_init();
	$timeout =3000;
	curl_setopt($ch, CURLOPT_TIMEOUT, 10);
	curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($ch, CURLOPT_HEADER, 1);
	curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,$timeout);
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_exec($ch);
	$httpcode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
	curl_close($ch);
	return $httpcode;
}
//ip 获取
function real_ip(){
$ip = $_SERVER['REMOTE_ADDR'];
if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
	foreach ($matches[0] AS $xip) {
		if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {
			$ip = $xip;
			break;
		}
	}
} elseif (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
	$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (isset($_SERVER['HTTP_CF_CONNECTING_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CF_CONNECTING_IP'])) {
	$ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
} elseif (isset($_SERVER['HTTP_X_REAL_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_X_REAL_IP'])) {
	$ip = $_SERVER['HTTP_X_REAL_IP'];
}
return $ip;
}
function ip_city_str($str){
	return str_replace(array('省','市'),'',$str);
}
function get_ip_city($ip)
{
    $url = 'http://whois.pconline.com.cn/ipJson.jsp?json=true&ip=';
    $city = curl_get($url . $ip);
	$city = mb_convert_encoding($city, "UTF-8", "GB2312");
    $city = json_decode($city, true);
    if ($city['city']) {
        $location = ip_city_str($city['pro']).ip_city_str($city['city']);
    } else {
        $location = ip_city_str($city['pro']);
    }
	if($location){
		return $location;
	}else{
		return false;
	}
}
//遍历目录
function del_dir($A){//自定义函数递归的函数整个目录
    if(file_exists($A)){//判断目录是否存在，如果不存在rmdir()函数会出错
        if($B=@opendir($A)){//打开目录返回目录资源，并判断是否成功;获取目录A下的文件或文件夹B、C
            while($filename=readdir($B)){//遍历目录B，读出目录中的文件或文件夹
                if($filename!='.' && $filename!='..'){//排除两个特殊的目录
                    $subFile=$A."/".$filename;//将目录下的文件与当前目录相连 A/B、A/C
                    if(is_dir($subFile)){//如果是目录条件则成了
                        del_dir($subFile);//递归调用自己(B文件夹)删除子目录
                        rmdir($subFile);//删除子目录后，删除该目录(B文件夹)
                    }
                    if(is_file($subFile)){//如果是文件条件则成立 ./A/a.php
                        unlink($subFile);//直接删除这个文件
                    }
                }
            }
            closedir($B);//关闭目录资源
            //rmdir($A);//删除A文件夹
        }
    }
}

function deldir($path)
{
	if (is_dir($path)) {
		$p = scandir($path);
		foreach ($p as $val) {
			if ($val != "." && $val != "..") {
				if (is_dir($path . $val)) {
					deldir($path . $val . '/');
					@rmdir($path . $val . '/');
				} else {
					unlink($path . $val);
				}
			}
		}
	}
}